﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Security.Principal;

namespace WirelessMigrator
{
    static class Helper
    {
        public static byte[] Compress(byte[] buffer)
        {
            byte[] gzBuffer = null;
            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    zip.Write(buffer, 0, buffer.Length);
                    zip.Close();
                    ms.Position = 0;

                    using (MemoryStream outStream = new MemoryStream())
                    {

                        byte[] compressed = new byte[ms.Length];
                        ms.Read(compressed, 0, compressed.Length);

                        gzBuffer = new byte[compressed.Length + 4];
                        Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
                        Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
                    }
                }
            }
            return gzBuffer;
        }

        public static byte[] Decompress(byte[] gzBuffer)
        {
            byte[] buffer;
            using (MemoryStream ms = new MemoryStream())
            {
                int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

                buffer = new byte[msgLength];

                ms.Position = 0;
                using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
                {
                    zip.Read(buffer, 0, buffer.Length);
                }
            }
            return buffer;
        }

        public static byte[][] SplitByteArrayByPattern(byte[] data, byte[] pattern)
        {
            List<int> al = new List<int>();
            byte[] _tmp = new byte[pattern.Length];
            for (int i = 0; i < data.Length - _tmp.Length + 1; i++)
            {
                Array.Copy(data, i, _tmp, 0, _tmp.Length);
                if (CompareByteArrays(_tmp, pattern))
                    al.Add(i);
            }
            byte[][] _resp = new byte[al.Count][];
            for (int j = 0; j < al.Count; j++)
            {
                int _tlen = 0;
                if (j == 0)
                    _tlen = (int)al[j];
                else
                    _tlen = (int)(al[j]) - ((int)al[j - 1] + pattern.Length);
                _resp[j] = new byte[_tlen];
                int spos = 0;
                if (j > 0)
                    spos = (int)al[j - 1] + pattern.Length;
                Array.Copy(data, spos, _resp[j], 0, _tlen);
            }

            return _resp;
        }
        
        public static bool CompareByteArrays(byte[] a1, byte[] a2)
        {
            return CompareByteArrays(a1, a2, true);
        }

        public static bool CompareByteArrays(byte[] a1, byte[] a2, bool checkLenght)
        {
            if (checkLenght && a1.Length != a2.Length)
                return false;
            for (int i = 0; i < Math.Min(a1.Length, a2.Length); i++)
            {
                if (a1[i] != a2[i])
                    return false;
            }
            return true;

        }

        internal static bool IsAdmin()
        {
            WindowsIdentity id = WindowsIdentity.GetCurrent();
            WindowsPrincipal p = new WindowsPrincipal(id);
            return p.IsInRole(WindowsBuiltInRole.Administrator);
        }
    }
}
